﻿using MediatR;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Domain;
using Swork03_28.Domain.Dto;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.TraceableHandler
{
    public class TraceableDetailCommandHandler : IRequestHandler<TraceableDetailCommand, TraceableDto>
    {
        private readonly ITraceableService traceableService;
        private readonly IGoodsService goodsService;

        public TraceableDetailCommandHandler(ITraceableService traceableService, IGoodsService goodsService)
        {
            this.traceableService = traceableService;
            this.goodsService = goodsService;
        }

        public async Task<TraceableDto> Handle(TraceableDetailCommand request, CancellationToken cancellationToken)
        {
            var info = await traceableService.GetAsync(request.Id);
            var gname = await goodsService.GetAsync(info.Gid);
            var res = new TraceableDto
            {
                Gid = info.Gid,
                Id = info.Id,
                Batch = info.Batch,
                CreatePeople = info.CreatePeople,
                CreateTime = info.CreateTime,
                Gname = gname.Name,
                Images = info.Images,
                TestImgs = info.TestImgs,
                Number = info.Number,
                ProduceEnterprise = info.ProduceEnterprise,
                Shelflife = info.Shelflife,
                UpdateePeople = info.UpdateePeople,
                UpdateTime = info.UpdateTime,
            };
            return res;
        }
    }
}
